<?php 
session_start();
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="public/CSS/works.css" type="text/css"/>
        <title>pham thanh anh chau</title>
    </head>
    <body>
        <div id="bg">
            <div class="forum">
                <?php include './html/header.php';?>
                
                <h1 align="center" style="margin-top: 82px">WORKS</h1>
                <?php
                require_once 'model/works.php';
                $worker = new Works();
                $work = $worker->select_edit(NULL, $_GET['id']);
                ?>
                <form  action="upload.php?id=<?php echo $_GET["id"] ?>" method="post" enctype="multipart/form-data">
                    <fieldset style="border: 2px #666666 solid">
                        <legend style="border: 3px #666666 solid; background: #6666ff; color: white">
                            Image Upload
                        </legend>
                        <label for="file">Filename:</label>
                        <input type="file" name="file" id="file"><br>
                        <label for="file">Name:</label>
                        <input type="text" name="name" value="<?php 
                                                                    if(!isset($_POST['submit']))
                                                                        echo $work["name"];
                                                                    else
                                                                        echo $_POST["name"];?>"><br>
                        <div style="float: left; width: 10%;"><label for="altText">Description</label></div>
                        <div style="float: left; width: 70%;">
                            <textarea name="des" rows="10" cols="100">
                            <?php 
                                if(!isset($_POST['submit']))
                                    echo $work['description'];
                                else 
                                    echo $_POST["des"] ;?>
                            </textarea>
                        </div><div style="clear: both"></div>
                        <input type="submit" name="submit" value="Submit">
                    </fieldset>
                </form>


                <?php
                if (isset($_POST['submit'])) {
                    $allowedExts = array("gif", "jpeg", "jpg", "png");
                    $temp = explode(".", $_FILES["file"]["name"]);
                    $extension = end($temp);
                    if ((($_FILES["file"]["type"] == "image/gif") 
                      || ($_FILES["file"]["type"] == "image/jpeg") 
                      || ($_FILES["file"]["type"] == "image/jpg") 
                      || ($_FILES["file"]["type"] == "image/pjpeg") 
                      || ($_FILES["file"]["type"] == "image/x-png")
                      || ($_FILES["file"]["type"] == "image/png"))
                      && in_array($extension, $allowedExts)) {
                        if ($_FILES["file"]["error"] > 0) {
//                            echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
                        } else {
//                            echo "Upload: " . $_FILES["file"]["name"] . "<br>";
//                            echo "Type: " . $_FILES["file"]["type"] . "<br>";
//                            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
//                            echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
                            $file_name = $temp[0] . '-' . time() . '.' . $temp[1];
                            if (file_exists("./public/Images/" . $_FILES["file"]["name"])) {
                                //echo $file_name . " already exists. " . "<br>";
                            } else {
                                move_uploaded_file($_FILES["file"]["tmp_name"], "./public/Images/" . $file_name);
                                //echo "Stored in: " . "./public/Images/" . $file_name . "<br>";
                                $array = array(
                                    "id" => $_GET["id"],
                                    "name" => $_POST["name"],
                                    "des" => $_POST["des"],
                                    "img" => "./public/Images/" . $file_name
                                );
                                $worker->update($array);
                            }
                            $img_url = "./public/Images/" . $file_name;
                        }
                    }else{
                        $array = array(
                                    "id" => $_GET["id"],
                                    "name" => $_POST["name"],
                                    "des" => $_POST["des"],
                                );
                                $worker->update_noimg($array);
//                    echo "<img src='$img_url' width='100px' >";
                    }
                }
                include './html/footer.php';
                ?>
            </div>
        </div>
    </body>
</html>